/*
 * @Author: Siogo 你的邮箱
 * @Date: 2025-11-03 11:11:44
 * @LastEditors: Siogo 你的邮箱
 * @LastEditTime: 2025-11-04 11:40:53
 * @FilePath: /frontend/src/api/tb/controlRule.ts
 * @Description: 设备接入控制规则API
 */
import { BasicModel, Page } from '../model/baseModel';
import { defHttp } from '/@/utils/http/axios';
import { EntityType } from '/@/enums/entityTypeEnum';

/**
 * 规则类型枚举
 */
export enum ControlRuleType {
  DEVICE_ID = 'DEVICE_ID',
  DEVICE_IP = 'DEVICE_IP',
}

/**
 * 规则模式枚举
 */
export enum ControlRuleMode {
  BLACKLIST = 'BLACKLIST',
  WHITELIST = 'WHITELIST',
}

/**
 * 规则对象接口
 */
export interface ControlRule extends BasicModel<EntityType.DEVICE> {
  tenantId?: EntityId<EntityType.TENANT>;
  type: ControlRuleType;
  value: string;
  mode: ControlRuleMode;
  description?: string;
}

/**
 * 查询参数接口
 */
export interface ControlRuleQueryParam {
  pageSize: number;
  page: number;
  textSearch?: string;
  sortProperty?: string;
  sortOrder?: string;
  mode?: ControlRuleMode;
  type?: ControlRuleType;
}

/**
 * 保存规则的请求参数
 */
export interface SaveControlRuleParam {
  ruleId?: string;
  type: ControlRuleType;
  value: string;
  mode: ControlRuleMode;
  description?: string;
}

/**
 * 获取设备接入控制规则列表
 */
export function getControlRuleList(params: ControlRuleQueryParam) {
  return defHttp.get<Page<ControlRule>>({
    url: '/api/deviceAccess/rules',
    params,
  });
}

/**
 * 根据ID获取规则详情
 */
export function getControlRuleById(ruleId: string) {
  return defHttp.get<ControlRule>({
    url: `/api/deviceAccess/rule/${ruleId}`,
  });
}

/**
 * 保存规则(创建或更新)
 */
export function saveControlRule(data: SaveControlRuleParam) {
  return defHttp.postJson<ControlRule>({
    url: '/api/deviceAccess/rule',
    data,
  });
}

/**
 * 删除规则
 */
export function deleteControlRule(ruleId: string) {
  return defHttp.delete<void>({
    url: `/api/deviceAccess/rule/${ruleId}`,
  });
}
